//
//  ConfirmViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-5-14.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "ConfirmViewController.h"
#import "NYMemberCenterDataManager.h"
#import "ServiceProtocolViewController.h"
#import "NSString+MD5.h"
#import "NYWebViewController.h"
@interface ConfirmViewController ()

@end

@implementation ConfirmViewController

@synthesize passwordField,resendButton,phoneLabel,checkBoxButton,agreeProtocolButton,registerButton;
@synthesize myBlanch;

#pragma mark - Private methods
- (void)enableResendButton
{
    self.resendButton.enabled = YES;
}

- (NSString *)checkPasswordValid
{
    if(passwordField.text.length == 0)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000001E"],@"密码"];
    else if(passwordField.text.length > 6)
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000002E"],@"密码"];
    else if(![passwordField.text isEqualToString:[NYMemberCenterDataManager sharedManager].loginMember.dynPwd])
        return [NSString stringWithFormat:[kRegisterPromptDict objectForKey:@"F000004E"],@"密码",@"获取的密码"];
    else
        return nil;
}

#pragma mark - IBActions
- (IBAction)resendButtonClicked:(id)sender
{
    [self.passwordField resignFirstResponder];
    self.resendButton.enabled = NO;
    [self performSelector:@selector(enableResendButton) withObject:nil afterDelay:30.f];
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,[NSString stringWithFormat:kDynamicPasswordUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.phoneNum,@"1"]];
    url = [url stringByAppendingString:@"app000022"];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:[NYMemberCenterDataManager sharedManager] purpose:kResendDynamicPasswordDownloaderKey];
}
- (IBAction)registerButtonClicked:(id)sender
{
    [self.passwordField resignFirstResponder];
    NSString *validPassword = [self checkPasswordValid];
    if(validPassword)
    {
        [[RYHUDManager sharedManager]showWithMessage:validPassword customView:nil hideDelay:2.f];
    }
    else
    {
        NSString *pwd = [NSString md5HexDigest:self.passwordField.text];
        NSString *subUrl = [NSString stringWithFormat:kRegisterUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.userName,[NYMemberCenterDataManager sharedManager].loginMember.phoneNum,pwd,self.myBlanch];
        subUrl = [subUrl stringByAppendingString:@"app000023"];
        NSString *registerUrl = [NSString stringWithFormat:@"%@%@",kServerAddress,subUrl];
        registerUrl = [registerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:registerUrl delegate:[NYMemberCenterDataManager sharedManager] purpose:kRegisterDownloaderKey];
        [[RYHUDManager sharedManager]startedNetWorkActivityWithText:@"注册中..."];
    }
}
- (IBAction)checkBoxButtonClicked:(id)sender
{
    self.checkBoxButton.selected = !self.checkBoxButton.selected;
    if(checkBoxButton.selected)
    {
        self.registerButton.enabled = YES;
    }
    else
    {
        self.registerButton.enabled = NO;
    }
}
- (IBAction)agreeProtocolButtonClicked:(id)sender
{
//    ServiceProtocolViewController *spvc = [[ServiceProtocolViewController alloc] init];
//    [self.navigationController pushViewController:spvc animated:YES];
//    [spvc release];
    NYWebViewController *nywvc = [[NYWebViewController alloc] init];
    nywvc.htmlType = kAboutServiceHtml;
    [self.navigationController pushViewController:nywvc animated:YES];
    [nywvc release];
}
#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(enableResendButton) object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Notification methods
- (void)resendDynPwdWithNotification:(NSNotification *)notification
{
    [[RYHUDManager sharedManager]showWithMessage:notification.object customView:nil hideDelay:2.f];
}

- (void)registerLoginDataSucceedWithNotification:(NSNotification *)notification
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

#pragma mark - UIViewController methods
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle:@"快速注册"];
    [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
    [self setRightNaviItemWithTitle:nil imageName:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resendDynPwdWithNotification:) name:kResendDynPwdResponseNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerLoginDataSucceedWithNotification:) name:kGetRegisterUserDataSuccessNotification object:nil];
    
    self.phoneLabel.text = [NYMemberCenterDataManager sharedManager].loginMember.phoneNum;
    [self performSelector:@selector(enableResendButton) withObject:nil afterDelay:30.f];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kResendDynamicPasswordDownloaderKey];
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kRegisterDownloaderKey];
        self.view = nil;
        self.passwordField = nil;
        self.resendButton = nil;
        self.phoneLabel = nil;
        self.checkBoxButton = nil;
        self.agreeProtocolButton = nil;
        self.registerButton = nil;
        self.myBlanch = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kResendDynamicPasswordDownloaderKey];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kRegisterDownloaderKey];
    self.passwordField = nil;
    self.resendButton = nil;
    self.phoneLabel = nil;
    self.checkBoxButton = nil;
    self.agreeProtocolButton = nil;
    self.registerButton = nil;
    self.myBlanch = nil;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kResendDynamicPasswordDownloaderKey];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:[NYMemberCenterDataManager sharedManager] purpose:kRegisterDownloaderKey];
    [passwordField release];
    [resendButton release];
    [phoneLabel release];
    [checkBoxButton release];
    [agreeProtocolButton release];
    [registerButton release];
    [myBlanch release];
    [super dealloc];
}

@end
